package cn.com.note;

import android.app.NotificationManager;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import cn.com.note.activity.CustomToastAc;
import cn.com.note.activity.EditViewActivity;
import cn.com.note.activity.FragActivity;
import cn.com.note.activity.HomeActivityJava;
import cn.com.note.activity.LoadImgActivity;
import cn.com.note.activity.LoginTxtActivity;
import cn.com.note.activity.PopViewActivity;
import cn.com.note.activity.RadioViewActivity;
import cn.com.note.activity.SelectViewActivity;
import cn.com.note.activity.SnackBarActivity;
import cn.com.note.activity.TableViewActivity;
import cn.com.note.activity.TipsActivity;
import cn.com.note.activity.TitleBarActivity;
import cn.com.note.activity.VersionUpdate;
import cn.com.note.contact.ContactsActivity;
import cn.com.note.notificationutils.NotificationUtils;
import cn.com.note.utils.DateUtils;
import cn.com.note.utils.JurisdictionUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private NotificationManager mManager;
    private Button sendn;
    public static final String id = "channel_1";
    public static final String name = "名字";
    private NotificationUtils notificationUtils;
    private EditText etTitle, etContent;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendn = findViewById(R.id.bt_notification);
        etTitle = findViewById(R.id.et_title);
        etContent = findViewById(R.id.et_content);
        imageView = findViewById(R.id.iv_img);
        findViewById(R.id.bt_notification_delete).setOnClickListener(this);
        findViewById(R.id.bt_select).setOnClickListener(this);
        findViewById(R.id.bt_radio).setOnClickListener(this);
        findViewById(R.id.bt_pop).setOnClickListener(this);
        findViewById(R.id.bt_editText).setOnClickListener(this);
        findViewById(R.id.bt_title_bar).setOnClickListener(this);
        findViewById(R.id.bt_insert).setOnClickListener(this);
        findViewById(R.id.bt_glide).setOnClickListener(this);
        findViewById(R.id.bt_table).setOnClickListener(this);
        findViewById(R.id.bt_createFile).setOnClickListener(this);
        findViewById(R.id.bt_login).setOnClickListener(this);
        findViewById(R.id.bt_frag).setOnClickListener(this);
        findViewById(R.id.bt_contact).setOnClickListener(this);
        findViewById(R.id.bt_SnackBar).setOnClickListener(this);
        findViewById(R.id.bt_update).setOnClickListener(this);
        findViewById(R.id.bt_update).setOnClickListener(this);
        findViewById(R.id.bt_toast).setOnClickListener(this);
        findViewById(R.id.bt_tips).setOnClickListener(this);
        sendn.setOnClickListener(this);
        notificationUtils = new NotificationUtils(this);
        JurisdictionUtils.verifyStoragePermissions(this);
        String path = Environment.getExternalStorageDirectory().getPath() + "/测试日志/log";
        LogUtils.getConfig()
                .setLogSwitch(true)
                .setFilePrefix("log")
                .setDir(new File(path))
                .setLog2FileSwitch(true);
        cn.com.note.utils.FileUtils.updateMediaLibrary(this, path);

        LogUtils.log(Log.INFO, "内容：", "首页启动");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_notification:
                notificationUtils.sendNotification(etTitle.getText().toString(), etContent.getText().toString());
                break;
            case R.id.bt_notification_delete:
                notificationUtils.deleteNotification(id);
                break;
            case R.id.bt_select:
                ActivityUtils.startActivity(SelectViewActivity.class);
                break;
            case R.id.bt_radio:
                ActivityUtils.startActivity(RadioViewActivity.class);
                break;
            case R.id.bt_editText:
                ActivityUtils.startActivity(EditViewActivity.class);
                break;
            case R.id.bt_pop:
                ActivityUtils.startActivity(PopViewActivity.class);
                break;
            case R.id.bt_title_bar:
                ActivityUtils.startActivity(TitleBarActivity.class);
                break;
            case R.id.bt_insert:
                ActivityUtils.startActivity(HomeActivityJava.class);
                break;
            case R.id.bt_table:
                ActivityUtils.startActivity(TableViewActivity.class);
                break;
            case R.id.bt_login:
                ActivityUtils.startActivity(LoginTxtActivity.class);
                break;
            case R.id.bt_glide:
                ActivityUtils.startActivity(LoadImgActivity.class);
                showImage();
                break;
            case R.id.bt_createFile:
                createFile();
                break;
            case R.id.bt_frag:
                ActivityUtils.startActivity(FragActivity.class);
                break;
            case R.id.bt_contact:
                ActivityUtils.startActivity(ContactsActivity.class);
                break;
            case R.id.bt_SnackBar:
                ActivityUtils.startActivity(SnackBarActivity.class);
                break;
            case R.id.bt_update:
                ActivityUtils.startActivity(VersionUpdate.class);
                break;
            case R.id.bt_toast:
                ActivityUtils.startActivity(CustomToastAc.class);
                break;
            case R.id.bt_tips:
                ActivityUtils.startActivity(TipsActivity.class);
                break;
            default:
                break;
        }
    }


    Drawable drawable;

    private void showImage() {
        String url = "http://pic33.photophoto.cn/20141210/0005018305025297_b.jpg";
//        Glide.with(this)
//                .load(url)
//                .asBitmap()
//                .centerCrop()
//                .into(imageView);
//        Glide.with(this).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
//            @Override
//            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                imageView.setImageBitmap(resource);
//            }
//        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                drawable = getDrawable("http://pic33.photophoto.cn/20141210/0005018305025297_b.jpg");
                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        imageView.setImageDrawable(drawable);
                    }
                });
            }
        }).start();


    }

    boolean mExternalStorageAvailable, mExternalStorageWriteable;

    private void createFile() {
        String path = getInternalPath("/hs/stst.txt");
        boolean is = FileUtils.createOrExistsDir(path);
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        }
        File distFile = new File(path);
        if (!distFile.getParentFile().exists()) {
            distFile.getParentFile().mkdirs();
        } else if (!distFile.isFile()) {
            try {
                distFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (is) {
            Toast.makeText(this, "创建成功！", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "创建失败！", Toast.LENGTH_SHORT).show();
        }

        File fileSD_file = new File(path);
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(fileSD_file));
        sendBroadcast(intent);

    }


    /**
     * 获取内部SD卡存储路径
     *
     * @param path
     * @return
     */

    public static String getInternalPath(String path) {
        return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
    }

    /**
     * 数据库表中一次插入多条记录
     */
    private void insertInDataBase() {
        SQLiteDatabase database = DateUtils.openDatabase(this, getInternalPath("/data.db"));
        if (database != null) {
            List<String> list = DateUtils.getData(this);
            int num = DateUtils.insertData(database, "T_PDM_CHECKOUT", list);
            Log.d("插入了数据", "mum:" + num);
        }
    }

    /**
     * 通过图片url生成Drawable对象
     *
     * @param urlpath
     * @return Bitmap
     * 根据url获取布局背景的对象
     */
    public static Drawable getDrawable(String urlpath) {
        Drawable drawable = null;
        try {
            URL url = new URL(urlpath);
            URLConnection conn = url.openConnection();
            conn.connect();
            InputStream in;
            in = conn.getInputStream();
            drawable = Drawable.createFromStream(in, "background.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return drawable;
    }


}
